package top.jnaw.jee.utils.nim;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.PropKit;
import java.util.HashMap;
import java.util.Map;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.utils.Https;

public class NIMSMS {

  private static final String CAPTCHA_CODE = PropKit
      .get("nim_captcha", "");
  private static final String NOTIFY_CODE = PropKit
      .get("nim_notify", "");
  private static final String INIT_CODE = PropKit
      .get("nim_init", "");
  private static final String CODE_LENGTH = PropKit
      .get("nim_code_length", "6");

  private static final String URL_CAPTCHA = "https://api.netease.im/sms/sendcode.action";
  private static final String URL_VERIFY_CAPTCHA = "https://api.netease.im/sms/verifycode.action";

  private static final String URL_NOTIFY = "https://api.netease.im/sms/sendtemplate.action";
  private static final String URL_QUERY_NOTIFY = "https://api.netease.im/sms/querystatus.action";

  private static final int CODE_SUCCESS = 200;

  public static String captcha(String mobile) {
    String captcha = null;

    Map<String, String> params = new HashMap<>();
    params.put("mobile", mobile);
    params.put("templateid", CAPTCHA_CODE);
    params.put("codeLen", CODE_LENGTH);

    String data = Https.post(URL_CAPTCHA,
        Utils.getHeaders(
            Consts.NIM_SMS_KEY, Consts.NIM_SMS_SECRET), params);

    JSONObject json = JSONObject.parseObject(data);
    if (CODE_SUCCESS == json.getInteger("code")) {
      captcha = json.getString("obj");
    }

    return captcha;
  }

  public static boolean verifyCaptcha(String mobile, String captcha) {
    Map<String, String> params = new HashMap<>();
    params.put("mobile", mobile);
    params.put("code", captcha);

    String data = Https.post(URL_VERIFY_CAPTCHA,
        Utils.getHeaders(
            Consts.NIM_SMS_KEY, Consts.NIM_SMS_SECRET), params);

    JSONObject json = JSONObject.parseObject(data);
    return (CODE_SUCCESS == json.getInteger("code"));
  }

  /**
   *
   */
  public static int init(JSONArray mobiles) {
    int result = -1;

    Map<String, String> params = new HashMap<>();
    params.put("mobiles", mobiles.toJSONString());
    params.put("params", mobiles.toJSONString());
    params.put("templateid", INIT_CODE);

    String data = Https.post(URL_NOTIFY,
        Utils.getHeaders(
            Consts.NIM_SMS_KEY, Consts.NIM_SMS_SECRET), params);

    JSONObject json = JSONObject.parseObject(data);
    if (CODE_SUCCESS == json.getInteger("code")) {
      result = json.getInteger("obj");
    }

    return result;
  }

  /**
   *
   */
  public static int notify(JSONArray mobiles, JSONArray content) {
    int result = -1;

    Map<String, String> params = new HashMap<>();
    params.put("mobiles", mobiles.toJSONString());
    params.put("params", content.toJSONString());
    params.put("templateid", NOTIFY_CODE);

    String data = Https.post(URL_NOTIFY,
        Utils.getHeaders(
            Consts.NIM_SMS_KEY, Consts.NIM_SMS_SECRET), params);

    JSONObject json = JSONObject.parseObject(data);
    if (CODE_SUCCESS == json.getInteger("code")) {
      result = json.getInteger("obj");
    }

    return result;
  }

  public static JSONArray queryNotify(int id) {
    JSONArray result = new JSONArray();

    Map<String, String> params = new HashMap<>();
    params.put("sendid", String.valueOf(id));

    String data = Https.post(URL_QUERY_NOTIFY,
        Utils.getHeaders(Consts.NIM_SMS_KEY, Consts.NIM_SMS_SECRET), params);

    JSONObject json = JSONObject.parseObject(data);

    if (CODE_SUCCESS == json.getInteger("code")) {
      JSONArray array = json.getJSONArray("obj");
      int size = array.size();

      for (int i = 0; i < size; ++i) {
        JSONObject object = array.getJSONObject(i);
        object.put("mobile", object.getString("mobile").substring(4));
//        object.put("status", 1 == object.getInteger("status"));
        result.add(object);
      }
    }
    return result;
  }
}
